<?php
class Model_Admin_Users extends Model{
    public function getInfo($name,$offset,$rows){
        $total= DB::select(array(DB::expr('count(1)'), 'total'))->from('users')->where("caption",'like','%'.$name.'%')->execute()->get("total");
        $sql="select t.usersid id,t.caption,t.createdon,t.createdby,t.remark
             from users t
             where t.caption like '%$name%'
             and t.isdeleted<>'1'
             order by t.CreatedOn desc
             limit $offset,$rows";
        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();
        return array("total"=>$total,"rows"=>$data);
    }
    public  function saveInfo($id,$caption,$remark){
        $total= DB::select(array(DB::expr('count(1)'), 'total'))->from('users')->where("caption",'=',$caption)->and_where('usersid','!=',$id)->execute()->get("total");
        if($total!="0"){
            return json_encode(array("info"=>"对不起，用户组已经存在！"));
        }
        if($id==""){
            return $this->add($caption,$remark);
        }
        else{
            return $this->alter($id,$caption,$remark);
        }
    }
    private function add($caption,$remark){
        try {
            list($insert_id, $total_rows)= DB::insert('users')->columns(array('caption','remark','CreatedBy','CreatedOn'))
                ->values(array($caption,$remark,Session::instance()->get('admin_username',NULL),date("Y-m-d h:i:sa")))->execute();
            Database::instance()->commit();
            return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
        }
        catch (Exception $e) {
            Database::instance()->rollback();
            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    private function alter($id,$caption,$remark){
        try {
            $total_rows=DB::update('users')->set(array('caption'=>$caption,'remark'=>$remark))
                ->where('usersid','=',$id)->execute();
            Database::instance()->commit();
            return json_encode(array("info"=>"success","count"=>$total_rows));
        }
        catch (Exception $e) {
            Database::instance()->rollback();
            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }

    public function addUser($usersid,$id){

        $sql="insert into user_group(userid,usersid)
              select sysuserid,'$usersid'
              from sysuser
              where sysuserid in($id)";

        DB::query(null,$sql)->execute();

        return json_encode(array("info"=>"success"));
    }

    public function removeUser($usersid,$id){

        $sql="delete from user_group
              where usersid='$usersid'
              and userid in($id)";

        DB::query(null,$sql)->execute();

        return json_encode(array("info"=>"success"));
    }

    public function addModel($usersid,$id){

        $sql="insert into user_model(sysmodelid,usersid)
              select sysmodelid,'$usersid'
              from sysmodel
              where sysmodelid in($id)";

        DB::query(null,$sql)->execute();

        return json_encode(array("info"=>"success"));
    }

    public function removeModel($usersid,$id){

        $sql="delete from user_model
              where usersid='$usersid'
              and sysmodelid in($id)";

        DB::query(null,$sql)->execute();

        return json_encode(array("info"=>"success"));
    }
}